
function toggleHostVisibility(elem){
    var f = scanWindowPanel.getForm().findField('scan_hostname');
    if(elem.checked){
        f.container.up('div.x-form-item').hide();
    }else{
        f.container.up('div.x-form-item').show();
    }
}

function getScanParams(){
    var hostControl = Ext.get('scan_hostname');
    var scanThisHostControl = Ext.getCmp('scan_this_host');
    if(scanThisHostControl.checked)
        return {};
    else
        return {server:hostControl.getValue()};
}

function showSucces(){
    Ext.supreme.showMsg('Skanowanie', 'Dodano Twój komputer do kolejki skanowania.');
}

function showFailure(){
    Ext.supreme.showMsg('B\u0142ąd', 'Nie mo\u017cna dodać Twójego komputera do kolejki skanowania.');
}

function scanCurrentHost(){
    Ext.Ajax.request({
        url: '/scanServer',
        method: 'POST',
        params: getScanParams,
        success: showSucces,
        failure: showFailure
    });
}

scanWindowPanel = undefined;

function buildScanPanel(){
    scanWindowPanel = new Ext.form.FormPanel({
        id: 'scan-form',
        labelAlign: 'left',
        border: false,
        bodyStyle:'padding:5px',
        layout: 'auto',    // Specifies that the items will now be arranged in columns
        items: [{
            columnWidth: 1,
            xtype: 'fieldset',
            //title: 'Skanowanie',
            labelWidth: 90,
            defaults: {
                width: 140,
                border:false
            },    // Default config options for child items
            defaultType: 'textfield',
            autoHeight: true,
            //bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
            border: false,
            style: {
                "margin-left": "10px", // when you add custom margin in IE 6...
                "margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0"  // you have to adjust for it somewhere else
            },
            items: [{
                xtype: 'checkbox',
                fieldLabel: 'Ten komputer',
                name: 'this_host',
                checked: false,
                id: 'scan_this_host',
                handler: toggleHostVisibility
            },{
                fieldLabel: 'Host',
                name: 'host',
                value: '127.0.0.1',
                itemCls: 'invisible',
                anchor: '100%',
                id: 'scan_hostname'
            }]
        }],
        keys:[{
                key: [Ext.EventObject.ENTER], handler: scanCurrentHost
            }],
        buttons:[{
                text: 'Skanuj host',
                xtype:'button',
                handler: scanCurrentHost,
                formBind:true
            }]
    });
    return scanWindowPanel;
}

Ext.supreme.ScanWindow = Ext.extend(Ext.Window, {
    
    windowTitle: 'Skanowanie',

    initComponent : function(){

        var defConfig = {
            host: undefined,
            width: 400,
            height: 200,
            title: this.windowTitle,
            layout: 'fit',
            items: [buildScanPanel()]
        };

        Ext.applyIf(this,defConfig);

        Ext.supreme.HostInfoWindow.superclass.initComponent.call(this);

    }
});



function showScanWindow(){
    var scanWindow = new Ext.supreme.ScanWindow();
    scanWindow.show();
}
